package com.xiaocq.wordskiller;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.xiaocq.wordskiller.fileIO.WordsReaderORWriter;

public class Main extends Activity {
	public static final String TAG = "Main";
	private static final int SHOW_INIT_DIALOG = 1;
	private static final int SHOW_EXIT_DIALOG = 2;
	private static final int SHOW_COOSE_DIALOG = 3;

	private Button gotoTest, gotoWrongSet, gotoStatistic, gotoSetting, gotoExit;
	private SharedPreferences pref;
	private ProgressDialog progressDialog;

	private WordsReaderORWriter wordsReader;
	
	private long startTimeInMillis,finishTimeInMillis;

	private Handler writeInHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			finishTimeInMillis = System.currentTimeMillis();
			progressDialog.dismiss();
			changeAppState();
			Toast.makeText(Main.this, R.string.tip_import_success,
					Toast.LENGTH_SHORT).show();
			Log.d(TAG, "耗时：" + (finishTimeInMillis - startTimeInMillis) + "ms");
		};
	};

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		pref = getSharedPreferences(CommandList.PREF_NAME,MODE_PRIVATE);
		boolean isFirst = pref.getBoolean(CommandList.IS_FIRST_START, true);
		if (isFirst) {
			chooseImportOrNot();
		}

		gotoExit = (Button) findViewById(R.id.main_gotoExit);
		gotoExit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(SHOW_EXIT_DIALOG);
			}
		});

		gotoTest = (Button) findViewById(R.id.main_gotoTest);
		gotoTest.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				showDialog(SHOW_COOSE_DIALOG);
			}
		});
		
		gotoStatistic = (Button) findViewById(R.id.main_gotoStatistic);
		gotoStatistic.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Main.this, Statistic.class);
				startActivity(intent);
			}
		});
		
		gotoWrongSet = (Button) findViewById(R.id.main_gotoWrongSet);
		gotoWrongSet.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(Main.this, UnitWrongSet.class);
				startActivity(intent);
			}
		});
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case SHOW_INIT_DIALOG:
			return new AlertDialog.Builder(Main.this)
					.setMessage(R.string.tip_first_using)
					.setPositiveButton(android.R.string.ok,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									initWordsFirstTime();
								}
							})
					.setNegativeButton(android.R.string.cancel,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).create();
		case SHOW_EXIT_DIALOG:
			return new AlertDialog.Builder(Main.this)
					.setMessage(R.string.tip_exit)
					.setPositiveButton(android.R.string.ok,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									finish();
									System.exit(0);
								}
							})
					.setNegativeButton(android.R.string.cancel,
							new OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
								}
							}).create();
		case SHOW_COOSE_DIALOG:
			return new AlertDialog.Builder(Main.this)
				.setItems(R.array.optional_or_unit, new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Intent intent = new Intent();
						if(which == 0){
							intent.setClass(Main.this, CostumTest.class);
							
						}else if(which == 1){
							intent.setClass(Main.this, UnitTest.class);
						}
						startActivity(intent);
					}
				}).create();
		default:
			return super.onCreateDialog(id);
		}
	}

	private void initWordsFirstTime() {
		wordsReader = new WordsReaderORWriter(this);
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage(getText(R.string.tip_loading));
		progressDialog.show();
		startTimeInMillis = System.currentTimeMillis();
		new Thread() {
			public void run() {
				InputStream inputStream;
				try {
					inputStream = getAssets().open("AllWords.txt");
					wordsReader.writeIntoDatabase(inputStream);
				} catch (IOException e) {
					e.printStackTrace();
				}
				writeInHandler.sendEmptyMessage(0);
			};
		}.start();
	}

	private void changeAppState() {
		SharedPreferences.Editor editor = pref.edit();
		editor.putBoolean(CommandList.IS_FIRST_START, false);
		editor.commit();
	}

	private void chooseImportOrNot() {
		showDialog(SHOW_INIT_DIALOG);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			showDialog(SHOW_EXIT_DIALOG);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}